﻿using ConfExpert.Enums;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace ConfExpert.Forms.Rapor
{
    public partial class Du4Barkod : DevExpress.XtraReports.UI.XtraReport
    {
        int EmptyLabelCount;
        string MyRange;
        public Du4Barkod(EtiketTipi tip, string range, int _emptyLabel = 0, bool tumu = false)
        {
            InitializeComponent();

            EmptyLabelCount = _emptyLabel;
            MyRange = range;
            if (tip == EtiketTipi.RollId)
                MyRange = MakeCriteria(range);

            if (MyRange != String.Empty)
            {
                System.Data.DataTable dt = Session.DataManager.ExecuteDataTable("GetBarcodesDu4", tip, tumu ? "0" : MyRange);
                dt.PrimaryKey = new DataColumn[] { dt.Columns["RollId"] };

                myData1.xEtiketDu4.Clear();
                myData1.EnforceConstraints = false;     //TODO: buna neden gerek kaldığı araştırılacak
                myData1.xEtiketDu4.Merge(dt);
            }
            //Eras Light ITC - Century - Microsoft Sans Serif
            if (myData1.xEtiketDu4.Rows.Count > 0)
                for (int i = 0; i < EmptyLabelCount; i++)
                {
                    ConfExpert.Datasets.MyData.xEtiketDu4Row r = myData1.xEtiketDu4.NewxEtiketDu4Row();
                    r.RollId = -(i + 1);
                    myData1.xEtiketDu4.Rows.InsertAt(r, 0);
                }

            if (tip == 0)
            {
                this.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
            }

            lbl1.BeforePrint += new PrintEventHandler(xrLabel_BeforePrint);
            lbl2.BeforePrint += new PrintEventHandler(xrLabel_BeforePrint);
            lbl3.BeforePrint += new PrintEventHandler(xrLabel_BeforePrint);
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            Session.DataManager.ExecuteNonQuery("UpdGPDu4Set", MyRange);
        }

        private static string MakeCriteria(string range)
        {
            string[] parts = range.Replace(" ", String.Empty).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string str = String.Empty;
            foreach (string part in parts)
            {
                try
                {
                    int loc = part.IndexOf("-");
                    if (loc > -1)
                    {
                        int p1 = int.Parse("0" + part.Substring(0, loc).Replace("-", ""));
                        int p2 = int.Parse("0" + part.Substring(loc, part.Length - loc).Replace("-", ""));
                        if (p1 == 0 && p2 > 0)
                            str += " OR r.RollId <= " + p2.ToString();
                        else if (p1 > 0 && p2 == 0)
                            str += " OR r.RollId >= " + p1.ToString();
                        else
                            str += " OR r.RollId between " + p1.ToString() + " and " + p2.ToString();
                    }
                    else
                        str += " OR r.RollId = " + int.Parse(part).ToString();
                }
                catch
                {
                }
            }
            return str.Substring(4);
        }

        private void xrLabel_BeforePrint(object sender, PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void ReSize(object sender)
        {
            int factor;

            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            if (this.ReportUnit == ReportUnit.HundredthsOfAnInch)
            {
                gr.PageUnit = GraphicsUnit.Inch;
                factor = 100;
            }
            else
            {
                gr.PageUnit = GraphicsUnit.Millimeter;
                factor = 10;
            }

            SizeF size = gr.MeasureString(((XRLabel)sender).Text, ((XRLabel)sender).Font);
            //((XRLabel)sender).Width = Convert.ToInt32(size.Width * factor);
            string fontName = ((XRLabel)sender).Font.Name;

            if (((XRLabel)sender).Height < size.Width * factor)
                ((XRLabel)sender).Font = new System.Drawing.Font(fontName, ((XRLabel)sender).Font.Size * ((XRLabel)sender).Height / size.Width / factor * 1.025F);

            gr.Dispose();
        }

        private void Detail_BeforePrint(object sender, PrintEventArgs e)
        {
            EmptyLabelCount--;
        }

        private void xrLabel_Hide(object sender, PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
                e.Cancel = true;
        }



    }
}